package com.ratta.supernote.plugin.sticker.lib.view.editText;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MenuItemView  extends TextView {
    private Paint mPaint;
    private RectF mRectF;
    private float mCornerRadius;
    public MenuItemView(Context context) {
        super(context);
        init();
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MenuItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        this.mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(getResources().getDimensionPixelSize(com.ratta.supernote.note.plugincore.R.dimen.dp_px_2));
        mPaint.setAntiAlias(true);
        mRectF = new RectF();
        // 设置圆角半径，这里使用8dp
        mCornerRadius = getResources().getDimensionPixelSize(com.ratta.supernote.note.plugincore.R.dimen.dp_px_4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制边框
        if(isPressed()) {
            mRectF.set(
                    mPaint.getStrokeWidth() / 2,  // 左
                    mPaint.getStrokeWidth() / 2,  // 上
                    getWidth() - mPaint.getStrokeWidth() / 2,  // 右
                    getHeight() - mPaint.getStrokeWidth() / 2   // 下
            );
            canvas.drawRoundRect(mRectF, mCornerRadius, mCornerRadius, mPaint);
        }
    }
}
